<!--班车方向列表页-->
<template>
    <div style="height: 100%;position: relative;">
        <m-page-list
                :option="option"
                :page="page"
                :total="total"
                :table-data="tableData"
                :table-loading="tableLoading"
                :page-loading="pageLoading"
                v-model:value="query"
                @h-btn-click="hBtnClick"
                @col-btn-click="colBtnClick"
                @search-data="searchData"

                @current-change="(val) => {isSeeDetail ? handleCurrentChange(val) : ''}"
        />
        <m-dialog-detail ref="dialogRef" :data="dialogData" v-model:show="dialogShow" :option="dialogOption" @confirm="dialogConfirm" ></m-dialog-detail>

        <m-drawer-submodule
                v-if="isSeeDetail"
                v-model:show="drawerShow"
                :group-list="dialogOption.groupList"
        >
        </m-drawer-submodule>
    </div>
</template>

<script setup>

  import {ref,reactive,onMounted,nextTick} from 'vue'
  //组件
  import MPageList from '@/components/PageList'
  import MDialogForm from '@/components/DialogForm/DialogForm'
  import MDialogDetail from '@/components/DialogForm/DialogDetail'
  import MDrawerSubmodule from '@/components/Submodule/DrawerSubmodule'
  //请求
  import {getZhangTao, addZhangTao, editZhangTao,delZhangTao,getZhangTaoMingxi, addZhangTaoMingxi, editZhangTaoMingxi,delZhangTaoMingxi,getEmployeeInf} from '@/request/api/basicData'

  import {usePageList} from "../../composables/pageList";
  import {refOpt} from "../const";
  import {getAuthority} from "../../utils/permission";

  let opts = {
    auth_id: 'zhangTaoHeSuanQuYu',
    getApi: getZhangTao,addApi:addZhangTao,editApi:editZhangTao,delApi:delZhangTao,
    hasDetail: true,
    dtlParamKey: 'zt_id',
    getDtlApi: getZhangTaoMingxi,
    addDtlApi: addZhangTaoMingxi,
    editDtlApi: editZhangTaoMingxi,
    delDtlApi: delZhangTaoMingxi,
    idKey: 'row_id'
  };
  let {pageLoading,option,page,total,tableLoading,tableData,query,dialogRef,dialogShow,dialogData,dialogOption,drawerShow,
    hBtnClick,colBtnClick,handleCurrentChange,searchData,getDataList,dialogConfirm} = usePageList(opts);

  option.conditions = [
    {label: '账套编号',type: 'text',field_id: 'zt_id',col: 12},
    {label: '账套名称',type: 'text',field_id: 'zt_name',col: 12},
  ];
  option.cols = [
    {prop: 'zt_id', label: '账套编号',},
    {prop: 'zt_name', label: '账套名称',},
    {prop: 'input_id',label: '录入员号' },
    {prop: 'input_name',label: '录入员名' },
    {prop: 'input_time',label: '录入时间' },
    {prop: 'modify_id',label: '修改员号' },
    {prop: 'modify_name',label: '修改员名' },
    {prop: 'modify_time',label: '修改时间' },
  ];
  dialogOption.groupList = [
    {
      title: '信息',
      fieldList: [
        {field_id: 'zt_id',label: '账套编号',type: 'text',col: 2 ,required: true},
        {field_id: 'zt_name',label: '账套名称',type: 'text',col: 2 ,required: true },
      ]
    },
    {
      title: '明细',
      isDetail: true,
      id: 'mingxi',
      dtlParamKey: 'zt_id',
      getDtlApi: getZhangTaoMingxi,
      addDtlApi: addZhangTaoMingxi,
      editDtlApi: editZhangTaoMingxi,
      delDtlApi: delZhangTaoMingxi,

      cols: [
        {...refOpt.job_no},
        {...refOpt.job_name}
        /*{field_id: 'job_no',label: '员工号',type: 'text',disabled: true },
        {field_id: 'job_name',label: '员工名',type: 'reference',refUrl: getEmployeeInf,

          refCols:[
            {prop: 'job_no',label: '员工编号' },
            {prop: 'job_name',label: '员工名称' },
          ],
          entityKey: 'job_name',valueKey: ['job_name','job_no'],
        },*/
      ],

      dltData: []
    },
  ];

  dialogConfirm = function (obj) {
    let {data,dtlData,option} = obj,
      isAdd = option.title === '新增';
    let pm = new Promise(resolve => {
      if (isAdd){
        addZhangTao(data).then(res => {
          if (res.errcode !== 0)return;
          resolve(data.zt_id);
        })
      }else {
        editZhangTao(data).then(res =>{
          if (res.errcode !== 0)return;
          resolve(data.zt_id);
        })
      }

    });

    pm.then((ztid) => {
      if (dtlData){
        let pms = [];
        dtlData.mingxi.forEach(item =>{
          pms.push(saveZTMing(ztid,item))
        });
        Promise.all(pms).then(res => {
          console.log(res);
          ElMessage({
            message: option.title+'成功',
            type: 'success',
          })
          dialogRef.value.close();
          getDataList();
        })
      }


    })
  }

  let isSeeDetail = ref(true);
  prepare();


  //保存账套明细
  function saveZTMing(ztid,data,isAdd) {
    return new Promise((resolve,reject) => {
      data.zt_id = ztid;
      if (!data.row_id) {
        addZhangTaoMingxi(data).then(res =>{
          if (res.errcode !== 0){
            reject();
            return;
          }
          resolve();
        })
      }else {
        editZhangTaoMingxi(data).then(res =>{
          if (res.errcode !== 0){
            reject();
            return;
          }
          resolve();
        })
      }
    })
  }


  function prepare() {
    let dtlAuth = getAuthority('zhangTaoCaoZuoYuanGongMingXi'),
      arr = [{
        title: '信息',
        fieldList: [
          {field_id: 'zt_id',label: '账套编号',type: 'text',col: 2 ,required: true},
          {field_id: 'zt_name',label: '账套名称',type: 'text',col: 2 ,required: true },
        ]
      }]
    let {is_see = true,is_append = true,is_edit = true,is_delete = true} = dtlAuth || {};
    isSeeDetail.value = is_see;
    arr.push({
      title: '明细',
      isDetail: true,
      id: 'mingxi',
      dtlParamKey: 'zt_id',
      getDtlApi: getZhangTaoMingxi,
      addDtlApi: addZhangTaoMingxi,
      editDtlApi: editZhangTaoMingxi,
      delDtlApi: delZhangTaoMingxi,
      dtlAuth: {is_see,is_append,is_edit,is_delete},
      dtlAuthId: 'zhangTaoCaoZuoYuanGongMingXi',
      cols: [
        {...refOpt.job_no},
        {...refOpt.job_name}
        /*{field_id: 'job_no',label: '员工号',type: 'text',disabled: true },
        {field_id: 'job_name',label: '员工名',type: 'reference',refUrl: getEmployeeInf,

          refCols:[
            {prop: 'job_no',label: '员工编号' },
            {prop: 'job_name',label: '员工名称' },
          ],
          entityKey: 'job_name',valueKey: ['job_name','job_no'],
        },*/
      ],

      dltData: []
    })
    dialogOption.groupList = arr;
  }


</script>

<style scoped>

</style>
